package org.yocastlong.mybatisplus.db.configuration;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yocastlong.mybatisplus.common.CommonConstants;
import org.yocastlong.mybatisplus.common.utils.HeaderContext;

import java.util.Optional;

/**
 * @author 朱海龙
 * 2025/6/9 21:59
 */
@Service
public class MyTenantLineHandle implements TenantLineHandler {

    @Autowired
    private TenantProperties tenantProperties;

    @Override
    public Expression getTenantId() {
        Object object = HeaderContext.getObject(CommonConstants.header.tenantId);
        return Optional.ofNullable(object)
                       .map(item -> new StringValue(String.valueOf(item)))
                       .orElse(new StringValue(CommonConstants.header.tenantId));
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return tenantProperties.getIgnoreTables()
                               .contains(tableName);
    }
}
